package com.camera.yl.v

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.camera.base.v.BaseActivity
import com.camera.base.v.BaseFragment
import com.camera.routemodule.RouterPath
import com.camera.yl.R
import com.camera.yl.adapter.HomePagerAdapter
import com.camera.yl.bean.TabBean
import com.camera.yl.databinding.ActivityFileBinding
import com.camera.yl.fragment.FileFragment
import com.camera.yl.v.models.FileViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@AndroidEntryPoint
@Route(path = RouterPath.path.file_activity)
class FileActivity: BaseActivity<ActivityFileBinding,FileViewModel>() {
    val TAG = FileActivity::class.java.simpleName
    private val dataList: MutableList<TabBean> = ArrayList<TabBean>()
    private var mFileFragment: FileFragment? = null
    
    override val viewModelClass: Class<FileViewModel>
        get() = FileViewModel::class.java
    override fun getLayoutId(): Int {
        return R.layout.activity_file
    }
    override fun getStatusBarColor(): Int{
        return R.color.title_background_color
    }

    override fun loadData() {
        dataList.clear()
        mBinding.viewModel = viewModel
        
        // 设置标题栏通用参数
        setupTitleBar()
        
        mFileFragment = FileFragment()
        var bundle = Bundle()
        bundle.putInt("type", 0)
        mFileFragment!!.arguments = bundle
        dataList.add(TabBean(getString(R.string.camera_fragment), mFileFragment))
        mFileFragment = FileFragment()
        bundle = Bundle()
        bundle.putInt("type",1)
        mFileFragment!!.arguments = bundle
        dataList.add(TabBean(getString(R.string.capture_fragment), mFileFragment))

        mBinding.viewpager.adapter = HomePagerAdapter(supportFragmentManager, dataList)
        mBinding.tablayout.setupWithViewPager(mBinding.viewpager, false)
        mBinding.viewpager.currentItem = 0
        mBinding.viewpager.offscreenPageLimit = dataList.size
        
        //设置TabItem的View(一定要在设置完适配器之后进行设置)
        mBinding.tablayout.addOnTabSelectedListener(object : com.google.android.material.tabs.TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: com.google.android.material.tabs.TabLayout.Tab) {
                // 如果当前在选择模式，切换tab时退出选择模式
                if (viewModel.isSelectionMode.value) {
                    viewModel.exitSelectionMode()
                }
                // 更新ViewModel的当前tab类型
                viewModel.setCurrentTab(tab.position)
            }

            override fun onTabUnselected(tab: com.google.android.material.tabs.TabLayout.Tab) {
            }

            override fun onTabReselected(tab: com.google.android.material.tabs.TabLayout.Tab) {
                //选中之后再次点击即复选时触发
            }
        })
        
        // 设置初始tab
        viewModel.setCurrentTab(0)
        
        // 观察选择模式状态变化
        observeSelectionModeStates()
    }

    override fun onBackPressed() {
        if((dataList.get(mBinding.viewpager.currentItem).fragment as BaseFragment<*>).onBack()) {
            super.onBackPressed()
        }
    }

    override fun initView() {
        // 使用数据绑定，不需要手动设置UI
    }
    
    /**
     * 设置标题栏通用参数和监听器
     */
    private fun setupTitleBar() {
        mBinding.titleLayout.apply {
            // 设置左侧点击监听器
            leftClickListener = View.OnClickListener {
                if (this@FileActivity.viewModel.isSelectionMode.value) {
                    this@FileActivity.viewModel.onCancelButtonClicked()
                } else {
                    this@FileActivity.onBackPressed()
                }
            }
            
            // 设置右侧点击监听器
            rightClickListener = View.OnClickListener {
                this@FileActivity.viewModel.onRightButtonClicked()
            }
        }
    }
    
    /**
     * 观察选择模式状态变化
     */
    private fun observeSelectionModeStates() {
        // 观察选择模式状态
        lifecycleScope.launch {
            viewModel.isSelectionMode.collectLatest { isSelectionMode ->
                updateTitleBarForSelectionMode(isSelectionMode)
            }
        }
        
        // 观察右侧按钮文本
        viewModel.rightText.observe(this) { text ->
            mBinding.titleLayout.rightText = text
        }
        
        // 观察右侧按钮可见性
        viewModel.rightTextVisibility.observe(this) { isVisible ->
            mBinding.titleLayout.rightTextVisibility = isVisible
        }
    }
    
    /**
     * 根据选择模式更新标题栏
     */
    private fun updateTitleBarForSelectionMode(isSelectionMode: Boolean) {
        val backIcon = if (isSelectionMode) {
            ContextCompat.getDrawable(this, R.drawable.icon_close)
        } else {
            ContextCompat.getDrawable(this, R.drawable.icon_back)
        }
        mBinding.titleLayout.backButtonIcon = backIcon
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}